home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS 1.31 / testcpp / 1 / test.g < prev   
Encoding:
Text File  |  1995-03-10  |  2.0 KB  |  73 lines  |  [TEXT/MPS ]

  1. /* This is test.g which tests a simple DLG-based scanner */
  2.  
  3. /* ANTLR will assign token type numbers (by creating an enum which you
  4.  * get in tokens.h)
  5.  */
  6.  
  7. <<
  8. /* user must define ANTLRToken here */
  9. class ANTLRToken : public DLGBasedToken {
  10.    /* The base class we have chosen holds only a token type. */
  11.  
  12. protected:
  13.     /* For our simple purposes, a token and a string is enough in
  14.      * our attribute */
  15.     ANTLRChar _text[30];
  16.  
  17. public:
  18.     ANTLRToken(TokenType t, ANTLRChar *s) : DLGBasedToken(t) { ; }
  19.     /* Your derived class MUST have a blank constructor. */
  20.     ANTLRToken() {;}
  21.  
  22.     ANTLRChar *getText() { return _text; }
  23.     void setText(ANTLRChar *s) { strncpy(_text, s, 30); }
  24.  
  25.     /* WARNING WARNING WARNING: you must return a stream of distinct tokens */
  26.     virtual ANTLRLightweightToken *makeToken(TokenType tt, ANTLRChar *txt, int line)
  27.         {
  28.             ANTLRToken *t = new ANTLRToken;
  29.             t->setType(tt); t->setText(txt); t->setLine(line);
  30.             return t;
  31.         }
  32. };
  33.  
  34. /* "DLGLexer" must match what you use on DLG command line (-cl);
  35.  * "DLGLexer" is the default.
  36.  */
  37. #include "DLGLexer.h"        /* include definition of DLGLexer.
  38.                              * This cannot be generated automatically because
  39.                              * ANTLR has no idea what you will call this file
  40.                              * with the DLG command-line options.
  41.                              */
  42.  
  43. int main()
  44. {
  45.     DLGFileInput in(stdin);    /* create an input stream for DLG to get chars from */
  46.     DLGLexer scan(&in);        /* create scanner reading from stdin */
  47.     ANTLRTokenBuffer pipe(&scan);/* make a buffered pipe between lexer & parser */
  48.     ANTLRToken aToken;        /* create a token to fill in for DLG */
  49.     scan.setToken(&aToken);
  50.     Expr parser(&pipe);        /* create a parser of type Expr hooked to the scanner */
  51.     parser.init();            /* init the parser; prime lookahead etc... */
  52.  
  53.     parser.e();                /* start parsing at rule 'e' of that parser */
  54.     return 0;
  55. }
  56. >>
  57.  
  58. #token "[\ \t\n]+"    <<skip();>>
  59. #token Eof "@"
  60.  
  61. #tokclass My { IDENTIFIER NUMBER }
  62.  
  63. class Expr {                /* Define a grammar class */
  64.  
  65. e    :    My My Eof
  66.         <<fprintf(stderr, "text is %s,%s\n", $1->getText(), $2->getText());>>
  67.     ;
  68.  
  69. }
  70.  
  71. #token IDENTIFIER    "[a-z]+"
  72. #token NUMBER        "[0-9]+"
  73.